{
 "cells": [
  {
   "cell_type": "markdown",
   "source": [
    "1.层级索引"
   ],
   "metadata": {
    "collapsed": false
   }
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {
    "collapsed": true,
    "ExecuteTime": {
     "end_time": "2024-07-16T01:30:45.590064600Z",
     "start_time": "2024-07-16T01:30:45.122860100Z"
    }
   },
   "outputs": [],
   "source": [
    "import numpy as np\n",
    "import pandas as pd"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "outputs": [
    {
     "data": {
      "text/plain": "a  0    0.670147\n   1    0.718151\n   2   -0.357210\nb  0   -0.538492\n   1   -0.064872\n   2   -0.232475\nc  0   -1.217941\n   1    1.029322\n   2   -1.138913\nd  0   -0.267465\n   1   -0.794674\n   2    1.127112\ndtype: float64"
     },
     "execution_count": 9,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "s1=pd.Series(np.random.randn(12),\n",
    "             index=[list('aaabbbcccddd'),\n",
    "                    [0,1,2,0,1,2,0,1,2,0,1,2]])\n",
    "s1"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2024-07-16T01:35:13.755270900Z",
     "start_time": "2024-07-16T01:35:13.746754200Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "<class 'pandas.core.indexes.multi.MultiIndex'>\n",
      "MultiIndex([('a', 0),\n",
      "            ('a', 1),\n",
      "            ('a', 2),\n",
      "            ('b', 0),\n",
      "            ('b', 1),\n",
      "            ('b', 2),\n",
      "            ('c', 0),\n",
      "            ('c', 1),\n",
      "            ('c', 2),\n",
      "            ('d', 0),\n",
      "            ('d', 1),\n",
      "            ('d', 2)],\n",
      "           )\n"
     ]
    }
   ],
   "source": [
    "print(type(s1.index))\n",
    "print(s1.index)"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2024-07-16T01:30:49.364275300Z",
     "start_time": "2024-07-16T01:30:49.355015300Z"
    }
   }
  },
  {
   "cell_type": "markdown",
   "source": [
    "2.选取"
   ],
   "metadata": {
    "collapsed": false
   }
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "outputs": [
    {
     "data": {
      "text/plain": "0    0.263463\n1   -0.738483\n2   -0.546200\ndtype: float64"
     },
     "execution_count": 8,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 1.外层选取\n",
    "s1['b']"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2024-07-15T14:06:47.268278800Z",
     "start_time": "2024-07-15T14:06:47.265746700Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "outputs": [
    {
     "data": {
      "text/plain": "a   -0.409946\nb   -0.546200\nc    0.099959\nd   -0.452043\ndtype: float64"
     },
     "execution_count": 9,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 2.内层获取\n",
    "s1[:,2]"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2024-07-15T14:12:33.134515400Z",
     "start_time": "2024-07-15T14:12:33.130495600Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "outputs": [
    {
     "data": {
      "text/plain": "np.float64(-0.2660625054922001)"
     },
     "execution_count": 10,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "s1['a',0]"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2024-07-15T14:13:08.405950900Z",
     "start_time": "2024-07-15T14:13:08.399070100Z"
    }
   }
  },
  {
   "cell_type": "markdown",
   "source": [],
   "metadata": {
    "collapsed": false
   }
  },
  {
   "cell_type": "markdown",
   "source": [
    "3.交换"
   ],
   "metadata": {
    "collapsed": false
   }
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "outputs": [
    {
     "data": {
      "text/plain": "a  0    1.537062\n   1    0.506827\n   2    0.410530\nb  0   -0.794967\n   1    0.287066\n   2    1.879985\nc  0    0.579036\n   1    0.182680\n   2   -0.997492\nd  0    0.079904\n   1    1.278677\n   2   -1.793588\ndtype: float64"
     },
     "execution_count": 7,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 1.swaplevel()交换内层和外层的索引\n",
    "s1.swaplevel()"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2024-07-16T01:31:31.665863700Z",
     "start_time": "2024-07-16T01:31:31.653315900Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "outputs": [
    {
     "data": {
      "text/plain": "a  0    0.670147\n   1    0.718151\n   2   -0.357210\nb  0   -0.538492\n   1   -0.064872\n   2   -0.232475\nc  0   -1.217941\n   1    1.029322\n   2   -1.138913\nd  0   -0.267465\n   1   -0.794674\n   2    1.127112\ndtype: float64"
     },
     "execution_count": 12,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 2.sortlevel()先对外层索引进行排序，再对内层索引进行排序,默认升序\n",
    "s1.sort_index()"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2024-07-16T01:37:02.863347100Z",
     "start_time": "2024-07-16T01:37:02.852705Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "outputs": [],
   "source": [
    "# 交换并排序分层\n",
    "s1.swaplevel().sort_index()"
   ],
   "metadata": {
    "collapsed": false
   }
  },
  {
   "cell_type": "markdown",
   "source": [],
   "metadata": {
    "collapsed": false
   }
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 2
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython2",
   "version": "2.7.6"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 0
}
